package com.example.shiwu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shiwu.adapter.MySimpleAdapter;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.db.vo.SendVo;
import com.example.shiwu.service.ItemsService;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItemInfoListActivity extends AppCompatActivity{
    private RefreshLayout mRefreshLayout;
    private ListView listView;
    private MySimpleAdapter myAdapter;

    private ItemsService itemsService;

    private Map<String, Object> showitem;
    private ArrayList<Items> itemLisAll = null;
    private int currentPage = 0;
    private int pageSize = 2;
    private int totalSize = 0;

    public ListView editSb;

    public TextView title_text,title_seting;
    public ImageView title_back;
    private Button item_sub_btn;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iteminfolist);

        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_seting = findViewById(R.id.title_setting);
//        item_sub_btn = findViewById(R.id.item_sub_btn);

        title_text.setText("项目列表");
        title_seting.setText("切换列表");
        itemLisAll = new ArrayList<Items>();
        title_seting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ItemListChangeActivity.class);
                v.getContext().startActivity(intent);
            }
        });

        itemsService = new ItemsService();
        //itemLis = new ArrayList<Items>();
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK,intent);//返回页面刷新数据
                ItemInfoListActivity.this.finish();
            }
        });
//        item_sub_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(v.getContext(), ItemInfoActivity.class);
//                v.getContext().startActivity(intent);
//            }
//        });

        this.getItemsData();

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() { //下拉刷新
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getItemsDataHeaderPull();
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { //上拉加载更多
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                getItemsDataFooterPull();
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });


        listView = (ListView) findViewById(R.id.list_view1);

    }
    public void updataData(){
        itemLisAll = new ArrayList<Items>();
        currentPage = 0;
        getItemsData();
        myAdapter.cleckAll();
    }
    private void getItemsData(){
        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(currentPage*pageSize),pageSize);
        if(itemLis.size()>0){
            currentPage ++;
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleAdapter(ItemInfoListActivity.this, itemLisAll, R.layout.iteminfolistitem);

            listView = findViewById(R.id.list_view1);
            listView.setAdapter(myAdapter);

            List<Items> itemLisSize = itemsService.getItems(new Items());
            totalSize = itemLisSize.size();
        }

    }

    private void getItemsDataHeaderPull(){
        currentPage =0;
        itemLisAll= null;
        itemLisAll = new ArrayList<Items>();
        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(0*pageSize),pageSize);
        if(itemLis.size()>0){
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleAdapter(ItemInfoListActivity.this, itemLisAll, R.layout.iteminfolistitem);

            listView = findViewById(R.id.list_view1);
            listView.setAdapter(myAdapter);

            currentPage ++;
            myAdapter.notifyDataSetChanged();
        }
    }
    private void getItemsDataFooterPull(){

        itemsService = new ItemsService();
        List<Items> itemLis = itemsService.getItemsList(new Items(),(currentPage*pageSize),pageSize);
        if(itemLis.size()>0){
            itemLisAll.addAll(itemLis);
            //创建一个simpleAdapter
            myAdapter = new MySimpleAdapter(ItemInfoListActivity.this, itemLisAll, R.layout.iteminfolistitem);

            listView = findViewById(R.id.list_view1);
            listView.setAdapter(myAdapter);

            currentPage ++;
            myAdapter.notifyDataSetChanged();
        }
    }

}